package com.example.administrator.reader.element;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.administrator.reader.R;
import com.example.administrator.reader.entity.Novel;
import com.example.administrator.reader.fragment.CityFragment;
import com.example.administrator.reader.utils.UIUtils;
import com.example.administrator.reader.view.BookView;
import com.example.administrator.reader.view.SListView;

import java.util.List;


public class CGdElement extends BaseElement<List<Novel>> {
    SListView lv;


    public CGdElement(Activity activity){
        this.activity = activity;
    }


    @Override
    public void initView() {
        view = View.inflate(UIUtils.getContext(), R.layout.e_gd, null);
        lv = (SListView) view.findViewById(R.id.lv);
    }

    @Override
    public void fillView() {
        lv.setAdapter(new GDAdapter());
    }

    private final int NORMAL_TYPE = 0;
    private final int FIRST_TYPE = 1;

    class GDAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            GDHolder gdHolder = null;
            ZhudaHolder zhudaHolder = null;
            Novel novel = data.get(position);
            switch (getItemViewType(position)) {
                case NORMAL_TYPE:
                    if (convertView == null) {
                        convertView = View.inflate(activity, R.layout.item_gd, null);
                        gdHolder = new GDHolder();
                        gdHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                        gdHolder.tvAuthor = (TextView) convertView.findViewById(R.id.tv_author);
                        gdHolder.tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
                        convertView.setTag(gdHolder);
                    } else {
                        gdHolder = (GDHolder) convertView.getTag();
                    }
                    gdHolder.tvTitle.setText(novel.getName());
                    gdHolder.tvAuthor.setText(novel.getAuthor());
                    gdHolder.tvDesc.setText(novel.getDesc());
                    break;
                case FIRST_TYPE:
                    if (convertView == null) {
                        convertView = View.inflate(activity, R.layout.item_zhuda, null);
                        zhudaHolder = new ZhudaHolder();
                        zhudaHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                        zhudaHolder.tvAuthor = (TextView) convertView.findViewById(R.id.tv_author);
                        zhudaHolder.tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
                        zhudaHolder.iv = (ImageView) convertView.findViewById(R.id.iv_book_cover);
                        zhudaHolder.bookView = (BookView) convertView.findViewById(R.id.book);
                        convertView.setTag(zhudaHolder);
                    } else {
                        zhudaHolder = (ZhudaHolder) convertView.getTag();
                    }
                    bu.display(zhudaHolder.iv, novel.getImgPath());
                    zhudaHolder.tvTitle.setText(novel.getName());
                    zhudaHolder.tvAuthor.setText(novel.getAuthor());
                    zhudaHolder.tvDesc.setText(novel.getDesc());

                    zhudaHolder.bookView.setiOpenBook(new BookView.IOpenBook() {
                        @Override
                        public int openBook(View v) {
//                            ListView listView = (ListView) v.getParent();
//                            int i = listView.indexOfChild(v);
                            List<Novel> zhuda = CityFragment.cityData.getGudians();
                            Novel novel = zhuda.get(0);
                            return novel.getId();
                        }
                    });

                    break;
            }
            return convertView;
        }

        @Override
        public int getViewTypeCount() {
            return super.getViewTypeCount() + 1;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return FIRST_TYPE;
            } else {
                return NORMAL_TYPE;
            }
        }
    }

    public class GDHolder {
        public TextView tvTitle, tvAuthor, tvDesc;
    }

    public class ZhudaHolder {
        public ImageView iv;
        public TextView tvTitle, tvAuthor, tvDesc;
        private BookView bookView;
    }

}

